import 'dart:async';

import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:online_study/theme/size.dart';
import 'package:video_player/video_player.dart';

//视频播放
class VideoPage extends StatefulWidget {
  String videoUrl;

  VideoPage(this.videoUrl);

  @override
  _VideoPageState createState() => _VideoPageState(videoUrl);
}

class _VideoPageState extends State<VideoPage> {
  VideoPlayerController videoPlayerController;
  ChewieController chewieController;
  String videoUrl;

  _VideoPageState(this.videoUrl);

  @override
  void initState() {
    super.initState();
    // 生成控制器(两个)
    videoPlayerController =
        VideoPlayerController.network('http://192.168.68.72:997/wb.mp4');
    Timer(Duration(milliseconds: 1000 * 6),
        () => {print(videoPlayerController.value.position.inMilliseconds)});
    chewieController = ChewieController(
      videoPlayerController: videoPlayerController,
      // 比例
      aspectRatio: 16 / 9,
      // 自动播放
      autoPlay: true,
      // 循环
      looping: true,
      // //控制器是否显示
      // showControls: true,
      // 占位图
      placeholder: new Container(
        color: Colors.black,
      ),
      // 是否在 UI 构建的时候就加载视频
      autoInitialize: false,
      // 拖动条样式颜色
      materialProgressColors: new ChewieProgressColors(
        playedColor: Colors.red,
        handleColor: Colors.blue,
        backgroundColor: Colors.grey,
        bufferedColor: Colors.lightGreen,
      ),
      deviceOrientationsOnEnterFullScreen: [DeviceOrientation.landscapeLeft],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ThemeSize.height160,
      child: Chewie(
        controller: chewieController,
      ),
    );
  }

  @override
  void dispose() {
    // 销毁播放器的控制器
    videoPlayerController.dispose();
    chewieController.dispose();
    super.dispose();
  }
}
